import { ref,computed } from 'vue'
import { defineStore } from 'pinia'

//定义store
export const useTasklistStore = defineStore('Tasklist', () => {
  var tasklist = ref([])

  //新增任务
  var addTask = (payload)=>{    //payload是任务名称
    tasklist.value.push({id:tasklist.value.length + 1,name:payload,status:false})
  }
  
  //更改任务状态
  var changeStatus = (payload)=>{   //payload是任务id
    var task = tasklist.value.find((item)=>{
      return item.id == payload
    })
    if(task){
      task.status = ! task.status
    }
  }

  //删除任务
  var deleteTask = (payload)=>{   //payload是任务id
    var index = tasklist.value.findIndex((item)=>{
      return item.id == payload
    })
    if(index != -1){
      tasklist.value.splice(index,1)
    }
  }

    //统计未完成的任务数量
    var unFinishedCount = computed(()=>{
      return tasklist.value.filter((item)=>!item.status).length;
    })

  return { 
    tasklist,
    addTask,
    changeStatus,
    deleteTask,
    unFinishedCount
  }
})
